<?php
namespace app\api\controller;

use app\api\model\Access;
use app\api\model\RoleAccess;
use think\facade\Request;
use app\api\logic\AuthLogic;
use think\facade\Db;
use app\api\model\Article;
class Auth extends ApiBase
{
    /**
     * @return \think\response\Json
     * 获取分类列表树状权限
     */
    public function getAuthList(\think\Request $request){

        $username=$request->token_user_name;

        $data = AuthLogic::getAccessData($username);
        foreach ($data as $k=>$v){
            if(empty($v['is_menu'])) unset($data[$k]);
        }
        $list = getTree($data,0);

        $top[]  = [
            'name'=>'顶级菜单',
            'id'=> '0',
            'parent_id'=>'0'
        ];
        $merge_list = array_merge($top,$list);

        return renderSuccess('',$merge_list);

    }

    /**
     * 获取权限列表
     */
    public function getAccessList(\think\Request $request){

        $username=$request->token_user_name;
//        $username='user1';
        $data = AuthLogic::getAccessData($username);

        $list = getTree($data,0);

        return renderSuccess('',$list);
    }

    public function add(){
        $data = Request::post();
        //验证
        foreach ($data['parent_id'] as $key=>$val){
            $data['parent_id'] = $val;
        }
        /*是否有角色选中了上级权限，选中则该权限也被选中*/
        $parent_choose = RoleAccess::where('access_id',$data['parent_id'])->column('role_id');


        Db::startTrans();
        try {
            $res = Access::add($data);

            if(!empty($parent_choose)){

                foreach ($parent_choose as $k=>$v){
                    $choss_data[]=[
                        'role_id'=>$v,
                        'access_id'=>$res->id,
                    ];
                }
                $roleAccess = new RoleAccess();
                $roleAccess->saveAll($choss_data);
            }
            return renderSuccess('添加成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return renderError($e->getMessage());
        }


    }

    public function edit(){
        $data = Request::post();

        if(!isset($data['id'])) return  renderError('缺少参数');
        if(is_array($data['parent_id'])){
            foreach ($data['parent_id'] as $key=>$val){
                $data['parent_id'] = $val;
            }
        }

        $parent_choose = RoleAccess::where('access_id',$data['parent_id'])->column('role_id');

        Db::startTrans();
        try {

            RoleAccess::where('access_id',$data['id'])->delete();
            $res  = Access::edit($data);
            if(!empty($parent_choose)){

                foreach ($parent_choose as $k=>$v){
                    $choss_data[]=[
                        'role_id'=>$v,
                        'access_id'=>$res->id,
                    ];
                }
                $roleAccess = new RoleAccess();
                $roleAccess->saveAll($choss_data);
            }
            return renderSuccess('更新成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return renderError($e->getMessage());
        }
//        if($res){
//            return renderSuccess('更新成功');
//        }else{
//            return renderError('更新失败');
//        }
    }


    public function del(){
        $data = Request::post();
        if(!isset($data['id'])) return  renderError('缺少参数');
        /*判断是否有下级权限*/
        $hasChild = Access::where('parent_id','=',$data['id'])->select();

        if(!$hasChild->isEmpty()){
            return renderError('请先删除下级权限');
        }
        Db::startTrans();
        try {
            /*删除权限*/
            Access::where('id','=',$data['id'])->delete();
            RoleAccess::where('access_id','=',$data['id'])->delete();
            // 提交事务
            Db::commit();
            return renderSuccess('删除成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return renderError('删除失败');
        }


    }

    public function getAccessById(){
        $data = Request::post();
        if(!isset($data['id'])) return  renderError('缺少参数');
        $data = Access::getDataById($data['id'])->toArray();
        if(empty($data)) return  renderError('数据不存在');
        return renderSuccess('',$data);
    }



}
